//
//  ina220.h
//  NMEA
//
//  Created by Hans Johnson on 2014-03-31.
//
//

#ifndef NMEA_ina220_h
#define NMEA_ina220_h

#define INA220a_ADDR 0x80
#define INA220b_ADDR 0x82

#define INA220_CNF      0x00
#define INA220_SHUNTV   0x01
#define INA220_VOLT     0x02
#define INA220_POWER    0x03
#define INA220_CURRENT  0x04
#define INA220_CAL      0x05

#define INA220_CNF_RST          (1<<15)
#define INA220_CNF_BRNG         (1<<13)
#define INA220_CNF_PGA_gm       (0x03<<11)
#define INA220_CNF_PGA_40mv     (0x00<<11)
#define INA220_CNF_PGA_80mv     (0x01<<11)
#define INA220_CNF_PGA_160mv    (0x02<<11)
#define INA220_CNF_PGA_320mv    (0x03<<11)
#define INA220_CNF_BADC_bp      7
#define INA220_CNF_SADC_bp      3
#define INA220_CNF_ADC_9bit     0x00
#define INA220_CNF_ADC_10bit    0x01
#define INA220_CNF_ADC_11bit    0x02
#define INA220_CNF_ADC_12bit    0x03
#define INA220_CNF_ADC_2AV      0x09
#define INA220_CNF_ADC_4AV      0x0A
#define INA220_CNF_ADC_8AV      0x0B
#define INA220_CNF_ADC_16AV     0x0C
#define INA220_CNF_ADC_32AV     0x0D
#define INA220_CNF_ADC_64AV     0x0E
#define INA220_CNF_ADC_128AV    0x0F

#define INA220_CNF_MODE_gm      (0x07<<0)
#define INA220_CNF_MODE_PD      0x00
#define INA220_CNF_MODE_SVT     0x01
#define INA220_CNF_MODE_BVT     0x02
#define INA220_CNF_MODE_SBVT    0x03
#define INA220_CNF_MODE_ADCOFF  0x04
#define INA220_CNF_MODE_SVC     0x05
#define INA220_CNF_MODE_BVC     0x06
#define INA220_CNF_MODE_SBVC    0x07

// Function to initialize the INA220
status_code_t ina220_init(uint8_t addr);

// Function to shut down the INA220
status_code_t ina220_shutdown(uint8_t addr);

// Function to run the INA220
status_code_t ina220_run(uint8_t addr);

// Read Value from ina220
status_code_t ina220_read(uint8_t addr, uint8_t reg, int16_t *value);

// Function to set the calibration data for the INA
void ina220_calibrate(uint8_t addr, int16_t curent, int16_t millivolts);

#endif
